package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiUserData;
import com.yanqu.road.entity.activity.youxing.ShanShuiBeauty;
import com.yanqu.road.entity.activity.youxing.ShanShuiGridParam;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouShanShuiUserDataDaoImpl extends TempDataObjectDao<CrossXingYouShanShuiUserData> {

    public static String getCrossXingYouShanShuiUserDataSql = "select * from `t_c_xing_you_user_data` where `activityId`=? and groupId = ?";
    public Map<Long, CrossXingYouShanShuiUserData> getCrossXingYouShanShuiUserData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossXingYouShanShuiUserData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouShanShuiUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXingYouShanShuiUserData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossXingYouShanShuiUserDataSql = "update `t_c_xing_you_user_data` set energy=?,useEnergy=?,teamId=?,energyLastTime=?,roleId=?,buyRole=?,mapTeamId=?," +
            "gridParam=?,driftGridParam=?,beautyList=?,lastAbilityUpdateTime=?,helpTimes=?,createTeamTimes=?," +
            "silverCoin=?,silverCoinTotal=?,silverUpdateTime=?,flyTimes=?,driftTimes=?,driftEndType=?,driftReward=?,startDriftTime=?," +
            "wishShareValue=?,wishShareLastGetTime=?,language=?,round=?,luckyTimes=?,misfortuneTimes=?,notTouchGrid=?,declaration=?,captorReward=? where activityId=?  and groupId = ? and userId=?";
    @Override
    protected boolean updateData(CrossXingYouShanShuiUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnergyLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoleId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getBuyRole(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMapTeamId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGridParam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDriftGridParam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBeautyList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastAbilityUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCreateTeamTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSilverCoin()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSilverCoinTotal()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSilverUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFlyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDriftTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDriftEndType()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(data.getDriftReward())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getStartDriftTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWishShareValue()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWishShareLastGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getLanguage()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLuckyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMisfortuneTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getNotTouchGrid(), ",")));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getDeclaration()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(data.getCaptorReward())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateCrossXingYouShanShuiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossXingYouShanShuiUserDataSql = "insert into `t_c_xing_you_user_data`(activityId,groupId,userId,energy,useEnergy,teamId,energyLastTime,roleId,buyRole,mapTeamId," +
            "gridParam,driftGridParam,beautyList,lastAbilityUpdateTime,helpTimes,createTeamTimes," +
            "silverCoin,silverCoinTotal,silverUpdateTime,flyTimes,driftTimes,driftEndType,driftReward,startDriftTime," +
            "wishShareValue,wishShareLastGetTime,language,round,luckyTimes,misfortuneTimes,notTouchGrid,declaration,captorReward) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouShanShuiUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnergyLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoleId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getBuyRole(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMapTeamId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGridParam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDriftGridParam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBeautyList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastAbilityUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCreateTeamTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSilverCoin()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSilverCoinTotal()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSilverUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFlyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDriftTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDriftEndType()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(data.getDriftReward())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getStartDriftTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWishShareValue()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWishShareLastGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getLanguage()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLuckyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMisfortuneTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getNotTouchGrid(), ",")));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getDeclaration()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(data.getCaptorReward())));
        if (getDb().executeNoneQuery(insertCrossXingYouShanShuiUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouShanShuiUserData parseData(ResultSet rs) throws SQLException {
        CrossXingYouShanShuiUserData data = new CrossXingYouShanShuiUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setEnergy(rs.getInt("energy"));
        data.setUseEnergy(rs.getInt("useEnergy"));
        data.setTeamId(rs.getInt("teamId"));
        data.setEnergyLastTime(rs.getLong("energyLastTime"));
        data.setRoleId(rs.getInt("roleId"));
        data.setBuyRole(StringUtils.stringToIntegerList(rs.getString("buyRole"), ","));
        data.setMapTeamId(rs.getInt("mapTeamId"));
        data.setGridParam(JSON.parseObject(rs.getString("gridParam"), ShanShuiGridParam.class));
        data.setDriftGridParam(JSON.parseObject(rs.getString("driftGridParam"), ShanShuiGridParam.class));
        data.setBeautyList(JSON.parseObject(rs.getString("beautyList"), new TypeReference<List<ShanShuiBeauty>>() {}));
        data.setLastAbilityUpdateTime(rs.getLong("lastAbilityUpdateTime"));
        data.setHelpTimes(rs.getInt("helpTimes"));
        data.setCreateTeamTimes(rs.getInt("createTeamTimes"));
        data.setSilverCoin(rs.getLong("silverCoin"));
        data.setSilverCoinTotal(rs.getLong("silverCoinTotal"));
        data.setSilverUpdateTime(rs.getLong("silverUpdateTime"));
        data.setFlyTimes(rs.getInt("flyTimes"));
        data.setDriftTimes(rs.getInt("driftTimes"));
        data.setDriftEndType(rs.getInt("driftEndType"));
        Property driftReward = PropertyHelper.parseStringToProperty(rs.getString("driftReward"));
        if(driftReward == null){
            data.setDriftReward(new Property());
        }else {
            data.setDriftReward(driftReward);
        }
        data.setStartDriftTime(rs.getLong("startDriftTime"));
        data.setWishShareValue(rs.getInt("wishShareValue"));
        data.setWishShareLastGetTime(rs.getLong("wishShareLastGetTime"));
        data.setLanguage(rs.getString("language"));
        data.setRound(rs.getInt("round"));
        data.setLuckyTimes(rs.getInt("luckyTimes"));
        data.setMisfortuneTimes(rs.getInt("misfortuneTimes"));
        data.setNotTouchGrid(StringUtils.stringToIntegerList(rs.getString("notTouchGrid"), ","));
        data.setDeclaration(rs.getString("declaration"));
        data.setCaptorReward(PropertyHelper.parseStringToProperty(rs.getString("captorReward")));
        data.setGroupId(rs.getInt("groupId"));
        return data;
    }
}
